// JavaScript Document

$(function() {
	
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	
	var username = $( "#registration-form-username" ),
		email = $( "#registration-form-email" ),
		password = $( "#registration-form-password" ),
		confirm_password = $( "#registration-form-confirm-password" ),
		allFields = $( [] ).add( username ).add( email )
										.add( password ).add( confirm_password ),
		tips = $( ".registration-form-validateTips" );


	function updateTips( t ) {
		tips
			.text( t )
			.addClass( "ui-state-highlight" );
		setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 1500 );
	}

	function checkLength( o, n, min, max ) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( "Length of " + n + " must be between " +
				min + " and " + max + "." );
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp( o, regexp, n ) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			updateTips( n );
			return false;
		} else {
			return true;
		}
	}
	
	function checkMatch( o1, o2 ) {
		if ( o1.val() != o2.val()) {
			o2.addClass( "ui-state-error" );
			updateTips( "Password and Confirm password must be identical." );
			return false;
		} else {
			return true;
		}
	}
	
	$( "#registration-form" ).dialog({
		autoOpen: false,
		height: 400,
		width: 350,
		modal: true,
		buttons: {
			"Create account": function() {
				var bValid = true;
				allFields.removeClass( "ui-state-error" );

				bValid = bValid && checkLength( username, "Username", 3, 16 );
				bValid = bValid && checkRegexp( username, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9 or underscore characters and begin with a letter." );
				
				bValid = bValid && checkLength( email, "Email", 6, 80 );
				bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. contact@imageene.org" );
				
				bValid = bValid && checkLength( password, "Password", 5, 20 );
				bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
				
				bValid = bValid && checkLength( confirm_password, "Confirm password", 5, 20 );
				bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
				bValid = bValid && checkMatch( password, confirm_password );

				if ( bValid ) {
					
					var dataString = 'username='+ username.val() + '&email=' + email.val() + '&password=' + password.val()  + '&confirmpassword=' + confirm_password.val();  
					alert (dataString);return false;  
					
					
					jQuery.ajax({  
					  type: "POST",  
					  url: "/auth/register",  
					  data: dataString,  
					  success: function() { 
						  alert("Send OK!");
//					    $('#contact_form').html("<div id='message'></div>");  
//					    $('#message').html("<h2>Contact Form Submitted!</h2>")  
//					    .append("<p>We will be in touch soon.</p>")  
//					    .hide()  
//					    .fadeIn(1500, function() {  
//					      $('#message').append("<img id='checkmark' src='images/check.png' />");  
//					    });  
					  },
					  error: function(e){
						  alert(e);
					  }
					});  
					  
					//$( "#registration-form-confirm" ).dialog( "open" );
					//$( this ).dialog( "close" );
				}
			},
			"Cancel": function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
	});
	
	$( "#registration-form-confirm" ).dialog({
		autoOpen: false,
		height: 190,
		width: 400,
		modal: false,
		buttons: {
			"Ok": function() {
				$( this ).dialog( "close" );
			}
		}
	});
	
	
});